package com.dudu.dao;

import java.sql.Connection;
import java.util.List;

import com.dudu.model.Role;

import net.sf.json.JSONObject;



import core.CoreDao;
import core.CoreDaoInvoke;
import core.db.DBConnectionUtil;

public class RoleDao {

	public static void main(String[] args) {

		CoreDao coreDao = new CoreDaoInvoke();
		/*RoleDao dao = new RoleDao();

		Role bean = new Role();
		bean.setDescription("管理员角色");
		bean.setName("管理员");
		
		dao.insert(coreDao, bean);
		dao.queryById(coreDao, 1);*/
		Connection conn;
		try {
			conn = DBConnectionUtil.getDBConnection("emate1");
			//coreDao.doExec(conn, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public int insert(CoreDao dao, Role bean) {

		Connection con = null;
		int result = -1;
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					"INSERT INTO t_role(name, description, create_time)");
			sql.append("VALUES(?, ?, sysdate())");

			List<JSONObject> list = dao.serviceJsonSql(
					con,
					sql.toString(),
					new Object[] { 
						bean.getName(), bean.getDescription()				
					});

			JSONObject obj = list.get(0);
			result = obj.optInt("result");

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	public Role queryById(CoreDao dao, int id) {

		Connection con = null;
		Role result = null;

		try {
			con = DBConnectionUtil.getDBConnection("emate1");

			StringBuffer sql = new StringBuffer(
					"select id, name, description, create_time FROM t_role where id=?");
			List<JSONObject> list = dao.serviceJsonSql(con, sql.toString(),
					new Object[] { id });
			if (list != null && list.size() > 0) {
				List<Role> items = dao.covertToBean(list, Role.class);
				result = items.get(0);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	public int update(CoreDao dao, Role bean) {

		Connection con = null;
		int result = -1;

		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					" UPDATE t_role set name=?, description=? ");
			sql.append(" where id=?");

			List<JSONObject> list = dao.serviceJsonSql(

					con,
					sql.toString(),
					new Object[] { 
						bean.getName(), bean.getDescription(), bean.getId()
					}

			);

			if (list != null && list.size() > 0) {
				list.get(0);
				JSONObject obj = list.get(0);
				result = obj.optInt("result");
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
	public List<JSONObject> queryAll(CoreDao dao) {

		Connection con = null;
		List<JSONObject> result = null;
		
		try {
			con = DBConnectionUtil.getDBConnection("emate1");
			StringBuffer sql = new StringBuffer(
					" select * from t_role order by create_time desc ");
			result = dao.serviceJsonSql(con, sql.toString(),
					new Object[] { null });

		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			DBConnectionUtil.close(null, null, con);
		}
		return result;
	}
	
}
